package com.example.antoherapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.administrator.aidldemo.IMyAidlInterface;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection{

    @BindView(R.id.btnStartservice)
    Button btnStartservice;
    @BindView(R.id.btnstopservice)
    Button btnstopservice;
    @BindView(R.id.btnbindservice)
    Button btnbindservice;
    @BindView(R.id.btnunbindservice)
    Button btnunbindservice;
    @BindView(R.id.tv_send)
    EditText tvSend;
    @BindView(R.id.btnsych)
    Button btnsych;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    private Intent anotherIntent;

    private IMyAidlInterface ibinder =null;
    private EditText tvsend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        anotherIntent = new Intent();
        anotherIntent.setComponent(new ComponentName("com.example.administrator.aidldemo", "com.example.administrator.aidldemo.APPService"));
        findViewById(R.id.btnStartservice).setOnClickListener(this);
        findViewById(R.id.btnstopservice).setOnClickListener(this);
        findViewById(R.id.btnbindservice).setOnClickListener(this);
        findViewById(R.id.btnunbindservice).setOnClickListener(this);
        findViewById(R.id.btnsych).setOnClickListener(this);
        tvsend = (EditText) findViewById(R.id.tv_send);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnStartservice:
                startService(anotherIntent);
                break;
            case R.id.btnstopservice:
                stopService(anotherIntent);
                break;
            case R.id.btnbindservice:
                bindService(anotherIntent,this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnunbindservice:
                unbindService(this);
                ibinder =null;
                break;
            case R.id.btnsych:

                if(ibinder!=null){
                    try {
                        ibinder.setData(tvsend.getText().toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }else{
                    System.out.println("ibinder为空");
                }
                break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        ibinder = IMyAidlInterface.Stub.asInterface(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}
